<?php 
defined('RUN_KATANA') or die('Restricted access');
class FormAdmin
{
	public $Info;
	public $Error;
	public $JsActionRun;
	
	public function FormAdmin()
	{
		self::runPreActions();
		self::runPostActions();
	}
	
	public function runPreActions()
	{
		global $commander;
		global $fc;
		switch($commander->getPostAction())
		{	
			case 'list_form':
				if($_POST['new_item_title'] != '')
				{ self::addForm($_POST['new_item_title']); }
				if($_POST['activate'] > 0)
				{ $commander->setTableActive($_POST['activate'], 'fm_forms', 1); }
				if($_POST['deactivate'] > 0)
				{ $commander->setTableActive($_POST['deactivate'], 'fm_forms', 0); }
				if($_POST['del_item_id'] > 0)
				{ self::deleteForm($_POST['del_item_id']); }
				return;
			break;
			
			case 'editor_form':
				if($_POST['up'] > 0)
				{ $commander->setTableItemOrderUp($_POST['up'], 'fm_form_fields', 'fieldOrder', 'formId'); }
				if($_POST['down'] > 0)
				{ $commander->setTableItemOrderDown($_POST['down'], 'fm_form_fields', 'fieldOrder', 'formId'); }
				if($_POST['activate'] > 0)
				{ $commander->setTableActive($_POST['activate'], 'fm_form_fields', 1); }
				if($_POST['deactivate'] > 0)
				{ $commander->setTableActive($_POST['deactivate'], 'fm_form_fields', 0); }
				return;
			break;
		}
	}
	
	public function runPostActions()
	{
		global $db;
		global $commander;
		global $fc;
		
		switch ($_POST['action'])
		{
			//List-Editor group actions
			case 'list_form':
				if((count($_POST['form_id']) > 0) && ($fc->getForms()))
				{
					$i = 0;
					foreach($_POST['form_id'] as $id)
					{
						if(isset($fc->Forms[$id]))
						{
							if($_POST['form_title'][$i] != $fc->Forms[$id]->getTitle())
							{ 
								$fc->Forms[$id]->setTitle($commander->formatText($_POST['form_title'][$i]));
								$fc->Forms[$id]->setDataChanged(true);
							}
							if($_POST['form_method'][$i] != $fc->Forms[$id]->getMethod())
							{ 
								$fc->Forms[$id]->setMethod(intval($_POST['form_method'][$i]));
								$fc->Forms[$id]->setDataChanged(true);
							}
							if($_POST['form_action'][$i] != $fc->Forms[$id]->getAction())
							{ 
								$fc->Forms[$id]->setAction($_POST['form_action'][$i]);
								$fc->Forms[$id]->setDataChanged(true);
							}
							if($_POST['MailTo'][$i] != $fc->Forms[$id]->getMailTo())
							{ 
								$fc->Forms[$id]->setMailTo($_POST['MailTo'][$i]);
								$fc->Forms[$id]->setDataChanged(true);
							}
							if($_POST['form_human_check'][$i] != $fc->Forms[$id]->getHumanCheck())
							{ 
								$fc->Forms[$id]->setHumanCheck($_POST['form_human_check'][$i]);
								$fc->Forms[$id]->setDataChanged(true);
							}
							$fc->Forms[$id]->update();
						}
						$i++;
					} //End foreach
					if($_POST['selected'])
					{
						foreach($_POST['selected'] as $id)
						{
							if(isset($fc->Forms[$id]))
							{
								if($_POST['activate'] == '-1')
								{
									$fc->Forms[$id]->setActive(1);
									$commander->setTableActive($id, 'fm_forms', 1); 
								}
								if($_POST['deactivate'] == '-1')
								{
									$fc->Forms[$id]->setActive(0);
									$commander->setTableActive($id, 'fm_forms', 0); 
								}
							}
						}
					}
				}
			break;
			//List-Editor group actions end
			
			//Editor Form actions
			case 'editor_form':
				if($fc->getCurrentFormId() > 0)
				{
					if(is_object($fc->getCurrentForm()))
					{
						if($_POST['new_item_title'] != '')
						{ $fc->getCurrentForm()->addFormField($commander->formatText($_POST['new_item_title'])); }

						if(count($_POST['ff_id']) > 0)
						{
							$fc->getCurrentForm()->loadFields();
							
							$i=0;
							foreach($_POST['ff_id'] as $id)
							{
								if(is_object($fc->CurrentForm->Fields[$id]))
								{
									if($_POST['ff_id'][$i] == $fc->CurrentForm->Fields[$id]->getId())
									{
										$field = $fc->CurrentForm->Fields[$id];
										if($_POST['ff_name'][$i] != $field->getName())
										{ 
											$field->setName($commander->formatText($_POST['ff_name'][$i]));
											$field->setDataChanged(true);
										}
										if($_POST['ff_label'][$i] != $field->getLabel())
										{ 
											$field->setLabel($commander->formatText($_POST['ff_label'][$i]));
											$field->setDataChanged(true);
										}
										if($_POST['ff_info'][$i] != $field->getInfo())
										{ 
											$field->setInfo($commander->formatHtml($_POST['ff_info'][$i]));
											$field->setDataChanged(true);
										}
										if($_POST['ff_type'][$i] != $field->getType())
										{ 
											$field->setType(intval($_POST['ff_type'][$i]));
											$field->setDataChanged(true);
										}
										if($_POST['ff_validType'][$i] != $field->getValidType())
										{ 
											$field->setValidType($_POST['ff_validType'][$i]);
											$field->setDataChanged(true);
										}
										if($_POST['ff_value'][$i] != $field->getValue())
										{ 
											$field->setValue($_POST['ff_value'][$i]);
											$field->setDataChanged(true);
										}
										if($_POST['ff_visible_'.$id] != $field->getVisible())
										{ 
											$field->setVisible(intval($_POST['ff_visible_'.$id]));
											$field->setDataChanged(true);
										}
										if($_POST['ff_req_'.$id] != $field->getReq())
										{ 
											$field->setReq(intval($_POST['ff_req_'.$id]));
											$field->setDataChanged(true);
										}
										if($_POST['ff_minLen'][$i] != $field->getType())
										{ 
											$field->setMinLen(intval($_POST['ff_minLen'][$i]));
											$field->setDataChanged(true);
										}
										if($_POST['ff_maxLen'][$i] != $field->getType())
										{ 
											$field->setMaxLen(intval($_POST['ff_maxLen'][$i]));
											$field->setDataChanged(true);
										}
										if($_POST['ff_nospace_'.$id] != $field->getNospace())
										{ 
											$field->setNospace(intval($_POST['ff_nospace_'.$id]));
											$field->setDataChanged(true);
										}
										if($_POST['ff_compare'][$i] != $field->getCompare())
										{ 
											$field->setCompare($commander->formatText($_POST['ff_compare'][$i]));
											$field->setDataChanged(true);
										}
										if($_POST['ff_active'][$i] != $field->getActive())
										{ 
											$field->setActive(intval($_POST['ff_active'][$i]));
											$field->setDataChanged(true);
										}
										if($_POST['ff_srcTable_'.$id] != $field->getSrcTable())
										{ 
											$field->setSrcTable($_POST['ff_srcTable_'.$id]);
											$field->setDataChanged(true);
										}
										if($_POST['ff_srcLblSelect_'.$id] != $field->getSrcLblSelect())
										{ 
											$field->setSrcLblSelect($_POST['ff_srcLblSelect_'.$id]);
											$field->setDataChanged(true);
										}
										if($_POST['ff_srcValSelect_'.$id] != $field->getSrcValSelect())
										{ 
											$field->setSrcValSelect($_POST['ff_srcValSelect_'.$id]);
											$field->setDataChanged(true);
										}
										if($_POST['ff_srcWhere_'.$id] != $field->getSrcWhere())
										{ 
											$field->setSrcWhere($_POST['ff_srcWhere_'.$id]);
											$field->setDataChanged(true);
										}
										if($_POST['ff_srcValDef_'.$id] != $field->getSrcValDef())
										{ 
											$field->setSrcValDef($_POST['ff_srcValDef_'.$id]);
											$field->setDataChanged(true);
										}
										if($_POST['ff_class'][$i] != $field->getClass())
										{ 
											$field->setClass($_POST['ff_class'][$i]);
											$field->setDataChanged(true);
										}
										if(($_POST['del_item_id'] > 0) && ($field->getId() == $_POST['del_item_id']))
										{ 
											if($fc->CurrentForm->deleteFormField($_POST['del_item_id']))
											{ unset($fc->CurrentForm->Fields[$id]); }
										}
									}
									$fc->CurrentForm->Fields[$id]->update();
								}
								$i++;
							}
						}
					}
				}
			break;
		}
	}
	
	
	function addForm($title)
	{
		global $commander;
		$title = $commander->formatText($title);
		if($title != '')
		{
			$sql  = 'INSERT INTO '.DB_PERFIX.'fm_forms (`title`)'; 
			$sql .= 'VALUES ("'.$title.'");';
			$result = mysql_query($sql);
		}
	}
	
	function deleteForm($id)
	{	
		if($id > 0)
		{	
			$sql = 'DELETE FROM '.DB_PERFIX.'fm_form_fields WHERE formId='.intval($id).';';
			$result = mysql_query($sql);
			$sql = 'DELETE FROM '.DB_PERFIX.'fm_forms WHERE id='.intval($id).' LIMIT 1;';
			$result = mysql_query($sql);
			return;
		}
	}
}
?>